﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Linq
{
    public static class FormatStringHelper
    {

        public static string ToFormatedStringWithSpacer<TItem>(this IQueryable<TItem> source, string spacer, string format, params Func<TItem, object>[] vals)
        {
            string[] tmpStrings = source.Select(item => string.Format(format, vals.Select(x => x.Invoke(item)).ToArray())).ToArray();
            return string.Join(spacer, tmpStrings);
        }

        public static string ToFormatedStringWithSpacer<TItem>(this IList<TItem> source, string spacer, string format, params Func<TItem, object>[] vals)
        {
            return source.AsQueryable().ToFormatedStringWithSpacer<TItem>(spacer, format, vals);
        }

        public static string ToFormatedStringWithSpacer<TItem>(this IEnumerable<TItem> source, string spacer, string format, params Func<TItem, object>[] vals)
        {
            return source.AsQueryable().ToFormatedStringWithSpacer<TItem>(spacer, format, vals);
        }

        public static string ToFormatedString<TItem>(this IQueryable<TItem> source, string format, params Func<TItem, object>[] vals)
        {
            return source.ToFormatedStringWithSpacer("", format, vals);
        }

        public static string ToFormatedString<TItem>(this IList<TItem> source, string format, params Func<TItem, object>[] vals)
        {
            return source.AsQueryable().ToFormatedStringWithSpacer("", format, vals);
        }

        public static string ToFormatedString<TItem>(this IEnumerable<TItem> source, string format, params Func<TItem, object>[] vals)
        {
            return source.AsQueryable().ToFormatedStringWithSpacer("", format, vals);
        }

        public static string ToFormatedStringWithSpacer<TItem>(this IQueryable<TItem> source, string spacer, params Func<TItem, object>[] vals)
        {
            //create format string in format "{0}, {1}
            var tmpFormatString = vals.AsQueryable().Select((x, index) => index).ToFormatedStringWithSpacer(", ", "{{{0}}}", x => x);
            return source.ToFormatedStringWithSpacer(spacer, tmpFormatString, vals);
        }

        public static string ToFormatedStringWithSpacer<TItem>(this IList<TItem> source, string spacer, params Func<TItem, object>[] vals)
        {
            return source.AsQueryable().ToFormatedStringWithSpacer(spacer, vals);
        }

        public static string ToFormatedStringWithSpacer<TItem>(this IEnumerable<TItem> source, string spacer, params Func<TItem, object>[] vals)
        {
            return source.AsQueryable().ToFormatedStringWithSpacer(spacer, vals);
        }

        public static string Format(this string source, params object[] args)
        {
            return string.Format(source, args);
        }

    }
}
